package com.sise.stuteaintersys.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sise.stuteaintersys.bean.base.ModifyResponse;
import com.sise.stuteaintersys.bean.entity.Admin;
import com.sise.stuteaintersys.bean.entity.CourseType;
import com.sise.stuteaintersys.bean.entity.Student;
import com.sise.stuteaintersys.bean.entity.Teacher;
import com.sise.stuteaintersys.bean.protocol.NetResult;
import com.sise.stuteaintersys.bean.vo.LoginRequestVO;
import com.sise.stuteaintersys.bean.vo.LoginResponseVO;
import com.sise.stuteaintersys.bean.vo.StudentInfoVo;
import com.sise.stuteaintersys.bean.vo.TeacherInfoVo;
import com.sise.stuteaintersys.locallibs.enums.AdminEnum;
import com.sise.stuteaintersys.locallibs.enums.FileEnum;
import com.sise.stuteaintersys.service.AdminCourseService;
import com.sise.stuteaintersys.service.AdminCourseTypeService;
import com.sise.stuteaintersys.service.AdminUserService;
import com.sise.stuteaintersys.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

/**
 * @ClassName AdminController
 * @Description
 * @Date 2018/12/30 10:27
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/admin")
@Slf4j
public class AdminController {

    @Autowired
    private AdminCourseTypeService adminCourseTypeService;

    @Autowired
    private AdminCourseService adminCourseService;

    @Autowired
    private LoginService loginService;

    @PostMapping(value = "/login/check")
    public LoginResponseVO checkLogin(LoginRequestVO loginRequestVO){
        return loginService.checkAdmin(loginRequestVO);
    }

    @Autowired
    private AdminUserService adminUserService;

    /**
     * 添加一个课程分类
     * @param courseType
     * @return
     */
    @PostMapping(value = "/addCourseType")
    public NetResult addCourseType(CourseType courseType){
        log.info(courseType.toString());
        if(adminCourseTypeService.addCourseType(courseType)){
            return NetResult.netresultok();
        }else {
            return NetResult.error(AdminEnum.ERROR.getCode(), AdminEnum.ERROR.getMessage());
        }
    }

    /**
     *  插入新的试题（一至多条）
     *
     *@param list
     *@return
     *@dateTime 2019/1/8 17:46
     */
    @PostMapping(value = "insert/question")
    public ModifyResponse insertHomework(String list) {
        System.out.println(list);
        return adminCourseTypeService.insertQuestion(list);
    }

    /**
     * 批量添加课程分类
     * @param list
     * @return
     */
    @PostMapping(value = "/addCourseTypeList")
    public NetResult addCourseTypeList(@Param("list") String list){
        log.info(list);
        if(adminCourseTypeService.addCourseTypeList(JSONArray.parseArray(list))){
            return NetResult.netresultok();
        }else {
            return NetResult.error(AdminEnum.ERROR.getCode(), AdminEnum.ERROR.getMessage());
        }
    }

    /**
     * 获取课程分类
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/courseTypeList")
    public NetResult getCourseTypeList(Integer pageNum, Integer pageSize){
        PageInfo<CourseType> info = adminCourseTypeService.checkAllCourseType(pageNum, pageSize);
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(), FileEnum.FILESUCCESS.getMessage(), info);
    }

    /**
     * 获取所有课程分类
     * @return
     */
    @GetMapping(value = "/allCourseTypeList")
    public NetResult getAllCourseType(){
        PageInfo<CourseType> list = adminCourseTypeService.checkAll();
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(), FileEnum.FILESUCCESS.getMessage(), list);
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    @GetMapping(value = "/getCourseType")
    public NetResult getCourseTypeById(Integer id){
        CourseType courseType = adminCourseTypeService.checkCourseType(id);
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(), FileEnum.FILESUCCESS.getMessage(), courseType);
    }

    /**
     * 根据id更新课程分类
     * @param courseType
     * @return
     */
    @PostMapping(value = "/updateCourseType")
    public NetResult updateCourseTypeById(CourseType courseType){
        if(adminCourseTypeService.updateCourseType(courseType)){
            return NetResult.netresultok();
        }else {
            return NetResult.error(AdminEnum.UNDETERRED.getCode(), AdminEnum.UNDETERRED.getMessage());
        }
    }

    /**
     * 根据课程分类id进行删除(有误，暂时不能使用)
     * @param courseType
     * @return
     */
    @PostMapping(value = "/delCourseType")
    public NetResult delCourseType(CourseType courseType){
        if(adminCourseTypeService.delCourseType(courseType)){
            return NetResult.netresultok();
        }else {
            return NetResult.error(AdminEnum.DEFERRER.getCode(), AdminEnum.DEFERRER.getMessage());
        }
    }

    /**
     * 查询所有课程信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/getAllCourseList")
    public NetResult getAllCourseList(Integer pageNum, Integer pageSize){
        PageInfo info = adminCourseService.checkAllCourse(pageNum, pageSize);
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(), FileEnum.FILESUCCESS.getMessage(), info);
    }

    /**
     * 直接查询所有课程信息
     * @return
     */
    @GetMapping(value = "/getAllCourse")
    public NetResult getAllCourse(){
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(), FileEnum.FILESUCCESS.getMessage(), adminCourseService.checkAll());
    }

    /**
     * 根据课程名字进行模糊查询
     * @param pageNum
     * @param pageSize
     * @param courseName
     * @return
     */
    @GetMapping(value = "/getCourseByCourseName")
    public NetResult checkCourseByCourseName(Integer pageNum, Integer pageSize, String courseName){
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                FileEnum.FILESUCCESS.getMessage(),
                adminCourseService.checkCourseByCourseName(pageNum, pageSize, courseName));
    }

    /**
     * 根据课程分类名字进行查询
     * @param pageNum
     * @param pageSize
     * @param courseTypeName
     * @return
     */
    @GetMapping(value = "/getCourseByCourseTypeName")
    public NetResult checkCourseByCourseTypeName(Integer pageNum, Integer pageSize, String courseTypeName){
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                FileEnum.FILESUCCESS.getMessage(),
                adminCourseService.checkCourseByCourseTypeName(pageNum, pageSize, courseTypeName));
    }

    /**
     * 根据课程分类名字查询所有课程
     * @param courseTypeName
     * @return
     */
    @GetMapping(value = "/getAllCourseByCourseTypeName")
    public NetResult checkAllCourseByCourseTypeName(String courseTypeName){
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                FileEnum.FILESUCCESS.getMessage(),
                adminCourseService.checkAllCourseByCourseTypeName(courseTypeName));
    }

    /**
     * 查询大表
     * @param pageNum
     * @param pageSize
     * @param courseName
     * @param typeName
     * @return
     */
    @GetMapping(value = "/getCourseAndType")
    public NetResult checkCourseAndType(@RequestParam(value = "pageNum")Integer pageNum,
                                        @RequestParam(value = "pageSize")Integer pageSize,
                                        String courseName, String typeName){
        log.info("pageNum, pageSize, courseName, typeName:"+pageNum+pageSize+courseName+typeName);
        //当类型不为空，课程名为空时，返回该类型的课程
        if(!StringUtils.isEmpty(typeName) && StringUtils.isEmpty(courseName)){
            return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                    FileEnum.FILESUCCESS.getMessage(),
                    adminCourseService.checkCourseByCourseTypeName(pageNum, pageSize, typeName));
        }
        //当课程名不为空，类型名为空时，返回该课程的模糊查询
        if(!StringUtils.isEmpty(courseName) && StringUtils.isEmpty(typeName)){
            return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                    FileEnum.FILESUCCESS.getMessage(),
                    new PageInfo<>(adminCourseService.checkCourseByCourseNameList(pageNum, pageSize, courseName)));
        }
        //当课程名和类型名均为null时
        PageHelper.startPage(pageNum, pageSize);
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                FileEnum.FILESUCCESS.getMessage(),
                new PageInfo<>(adminCourseService.checkCourseAndType(pageNum, pageSize)));
    }

    /**
     * 获取学生信息
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping(value = "/getStudents")
    public NetResult getStudents(@RequestParam(value = "pageNum")Integer pageNum,
                                 @RequestParam(value = "pageSize")Integer pageSize,
                                 String name){
        if(StringUtils.isEmpty(name)){
            return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                    FileEnum.FILESUCCESS.getMessage(),
                    adminUserService.findStudents(pageNum, pageSize));
        }else {
            return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                    FileEnum.FILESUCCESS.getMessage(),
                    adminUserService.findStudentByName(pageNum, pageSize, name));
        }
    }

    /**
     * 获取教师信息
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping(value = "/getTeachers")
    public NetResult getTeachers(@RequestParam(value = "pageNum")Integer pageNum,
                                 @RequestParam(value = "pageSize")Integer pageSize,
                                 String name){
        if(StringUtils.isEmpty(name)){
            return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                    FileEnum.FILESUCCESS.getMessage(),
                    adminUserService.findTeachers(pageNum, pageSize));
        }else {
            return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                    FileEnum.FILESUCCESS.getMessage(),
                    adminUserService.findTeacherByName(pageNum, pageSize, name));
        }
    }

    /**
     * 获取管理员信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/getAdmin")
    public NetResult getAdmin(@RequestParam(value = "pageNum")Integer pageNum,
                              @RequestParam(value = "pageSize")Integer pageSize){
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                FileEnum.FILESUCCESS.getMessage(),
                adminUserService.findAdmins(pageNum, pageSize));
    }

    /**
     * 添加学生
     * @param studentInfoVo
     * @return
     */
    @PostMapping(value = "/addStudent")
    public NetResult addStudent(StudentInfoVo studentInfoVo) throws ParseException {
        if(adminUserService.checkUser(studentInfoVo.getLoginName(), "student")>0){
            return NetResult.error(AdminEnum.LOGINNAMEERROR.getCode(), AdminEnum.LOGINNAMEERROR.getMessage());
        }
        Student student = new Student();
        if(!StringUtils.isEmpty(studentInfoVo.getBirth())) {
            Date birthday = DateUtils.parseDate(studentInfoVo.getBirth(), Locale.TRADITIONAL_CHINESE, "yyyyMMdd");
            BeanUtils.copyProperties(studentInfoVo, student);
            student.setBirthday(birthday);
        }else {
            BeanUtils.copyProperties(studentInfoVo, student);
        }
        System.out.println(student.toString());
        if(adminUserService.addStudent(student)){
            return NetResult.netresultok();
        }else {
            return NetResult.error(AdminEnum.STUDENT.getCode(), AdminEnum.STUDENT.getMessage());
        }
    }

    /**
     * 添加教师
     * @param teacherInfoVo
     * @return
     */
    @PostMapping(value = "/addTeacher")
    public NetResult addTeacher(TeacherInfoVo teacherInfoVo) throws ParseException {
        if(adminUserService.checkUser(teacherInfoVo.getName(), "teacher")>0){
            return NetResult.error(AdminEnum.LOGINNAMEERROR.getCode(), AdminEnum.LOGINNAMEERROR.getMessage());
        }
        Teacher teacher = new Teacher();
        if(!StringUtils.isEmpty(teacherInfoVo.getBirth())) {
            Date birthday = DateUtils.parseDate(teacherInfoVo.getBirth(), Locale.TRADITIONAL_CHINESE, "yyyyMMdd");
            BeanUtils.copyProperties(teacherInfoVo, teacher);
            teacher.setBirthday(birthday);
        }else {
            BeanUtils.copyProperties(teacherInfoVo, teacher);
        }
        if(adminUserService.addTeacher(teacher)){
            return NetResult.netresultok();
        }else {
            return NetResult.error(AdminEnum.TEACHER.getCode(), AdminEnum.TEACHER.getMessage());
        }
    }

    /**
     * 添加管理员
     * @param admin
     * @return
     */
    @PostMapping(value = "/addAdmin")
    public NetResult addAdmin(Admin admin){
        if(adminUserService.addAdmin(admin)){
            return NetResult.netresultok();
        }else {
            return NetResult.error(AdminEnum.ADMIN.getCode(), AdminEnum.ADMIN.getMessage());
        }
    }

    /**
     * 删学生
     * @param id
     * @return
     */
    @PostMapping(value = "/delStudent")
    public NetResult delStudent(Integer id){
        if(adminUserService.delStudent(id) == 1) {
            return NetResult.netresultok();
        }else {
            return NetResult.error(AdminEnum.DELSTUDENT.getCode(), AdminEnum.DELSTUDENT.getMessage());
        }
    }

    /**
     * 删教师
     * @param id
     * @return
     */
    @PostMapping(value = "/delTeacher")
    public NetResult delTeacher(Integer id){

        if(adminUserService.delTeacher(id) == 1) {
            return NetResult.netresultok();
        }else {
            return NetResult.error(AdminEnum.DELTEACHER.getCode(), AdminEnum.DELTEACHER.getMessage());
        }
    }

}
